package cc.messcat.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

	public static final String DATE_DEFAULT_FORMAT = "yyyy-MM-dd";
	
	public static final String DATETIME_DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
	
	public static final String TIME_DEFAULT_FORMAT = "HH:mm:ss";
	
	
	/**
	 * 字符串日期转换为Date类型
	 * @param date
	 * @param format
	 * @return
	 */
	public static Date convertToDate(String date, String format){
		try{
			 return new SimpleDateFormat(format).parse(date);
		}catch(ParseException e){
			 e.printStackTrace();
		}
		 return null;
		
	}
	 
	 /**
	 * 将日期按指定的格式转换为字符串
	 * @param date
	 * @param format
	 * @return
	 */
	 public static String convertToString(Date date,String format) {
	     	 return new SimpleDateFormat(format).format(date);
     }
	 
	 /**
	  * 根据年月日获取日期Date
	  * @param year
	  * @param month
	  * @param date
	  * @return
	  */
	 public static Date getDate(Integer year,Integer month,Integer date){
			Calendar calendar=Calendar.getInstance();
			calendar.set(year, month, date);
			return  calendar.getTime();
	 }
	 
	 /**
	  * 获取有年月日时分秒组成的字符串(24小时制的)
	  * @return
	  */
	 public static String  getDateSerialNumber(){
		 Calendar cal=Calendar.getInstance();
		 cal = Calendar.getInstance();
		 StringBuffer serialNumber=new StringBuffer();
		 serialNumber.append(cal.get(Calendar.YEAR))
		 .append(cal.get(Calendar.MONTH) + 1)
		 .append(cal.get(Calendar.DAY_OF_MONTH))
		 .append(cal.get(Calendar.HOUR_OF_DAY))
		 .append(cal.get(Calendar.MINUTE))
		 .append(cal.get(Calendar.SECOND));
		 return serialNumber.toString();
	 }
	 
	    /** 
	     *  
	     * @param date 
	     * @return 
	     * @description 得到年份 
	     * @version currentVersion 
	     */  
	 public static String getYear(Date date) {  
	        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
	        String year = formatter.format(date);  
	        return year;  
	 }  
	 
	 /**
	  * 获取月份 
	  * @param date
	  * @return
	  */
	 public static String getMonth(Date date) {  
	        SimpleDateFormat formatter = new SimpleDateFormat("M");  
	        String month = formatter.format(date);  
	        return month;  
	 }  
	 
	 /**
	  * 获取日期
	  * @param date
	  * @return
	  */
	 public static String  getDate(Date date){
		 SimpleDateFormat formatter = new SimpleDateFormat("dd");
		 return formatter.format(date); 
	 }
	 
	 public static void main(String[] args) {
		System.out.println(System.currentTimeMillis());
		System.out.println(DateUtil.getDateSerialNumber());
		System.out.println(DateUtil.getYear(new Date()));
		System.out.println(DateUtil.getMonth(new Date()));
		System.out.println(DateUtil.getDate(new Date()));
	}
}
